package targaman.moltenwow.autovoter.controllers;

import org.springframework.stereotype.Controller;
import targaman.moltenwow.autovoter.Logger.MoltenLogger;
import targaman.moltenwow.autovoter.lang.Constants;
import targaman.moltenwow.autovoter.services.Configuration;

/**
 * Created by Jonathan on 30/12/2014.
 */

@Controller(value = Constants.CONFIGURATION_SERVICE)
public class ConfigurationImpl extends Configuration {

    public static final String SET_AUTO_VOTE = "setAutoVote";
    public static final String SET_AUTO_LOGIN = "setAutoLogin";
    public static final String SET_ENABLE_CONSOLE = "setEnableConsole";
    public static final String MINIMISE_TO_TRAY_ON_STARTUP = "minimiseToTrayOnStartup";
    public static final String DEFAULT_USERNAME = "defaultUsername";
    public static final String DEFAULT_PASSWORD = "defaultPassword";

    @Override
    public void setup(){
        if(getConfigurationFile()) {
            checkValidConfigurationFile();
            MoltenLogger.Log("Configuration File Loaded!");
        }
        else {
            setAutoVote(false);
            setAutoLogin(false);
            setEnableConsole(true);
            setMinimiseToTrayOnStartup(false);
            MoltenLogger.Log("Created Configuration File!");
        }
    }

    public void checkValidConfigurationFile() {
        if(!hasConfigurationInList(SET_AUTO_VOTE))
            setAutoVote(false);
        if(!hasConfigurationInList(SET_AUTO_LOGIN))
            setAutoLogin(false);
        if(!hasConfigurationInList(SET_ENABLE_CONSOLE))
            setEnableConsole(true);
        if(!hasConfigurationInList(MINIMISE_TO_TRAY_ON_STARTUP))
            setMinimiseToTrayOnStartup(false);
    }

    public void setDefaultLogin(String userName, String password) {
        addToConfigurationList(DEFAULT_USERNAME, encryptString(userName));
        addToConfigurationList(DEFAULT_PASSWORD, encryptString(password));
        createOrReplaceConfigurationFile();
    }

    public void setAutoVote(boolean value) {
        addToConfigurationList(SET_AUTO_VOTE, String.valueOf(value));
        createOrReplaceConfigurationFile();
    }

    public void setAutoLogin(boolean value) {
        addToConfigurationList(SET_AUTO_LOGIN, String.valueOf(value));
        createOrReplaceConfigurationFile();
    }

    public void setDefaultUsername(String value) {
        addToConfigurationList(DEFAULT_USERNAME, value);
        createOrReplaceConfigurationFile();
    }

    public void setDefaultPassword(String value) {
        addToConfigurationList(DEFAULT_PASSWORD, value);
        createOrReplaceConfigurationFile();
    }

    public void setEnableConsole(boolean value) {
        addToConfigurationList(SET_ENABLE_CONSOLE, String.valueOf(value));
        createOrReplaceConfigurationFile();
    }

    public void setMinimiseToTrayOnStartup(boolean value) {
        addToConfigurationList(MINIMISE_TO_TRAY_ON_STARTUP, String.valueOf(value));
        createOrReplaceConfigurationFile();
    }
}
